class OpenApi::SmsController < ApplicationController
#  before_filter :authorize, :only => :new
 
  def authorize
  end
   
  def new
  end
  
  def create    
    @user = User.find_by_nick_name_or_email_or_mobile(params[:mobile])    
    if @user.nil?
      render :xml => "<?xml version='1.0' encoding='UTF-8'?><errors><error>USER_INVALID</error></errors>" 
    elsif not @user.check_password(params[:password])    
      render :xml => "<?xml version='1.0' encoding='UTF-8'?><errors><error>PASSWORD_ERROR</error></errors>" 
    elsif not @user.mobile?
      render :xml => "<?xml version='1.0' encoding='UTF-8'?><errors><error>NO_ROBOT</error></errors>" 
    else
      begin
        render :xml => "<?xml version='1.0' encoding='UTF-8'?><result>#{@user.try_to_send_sm_by_lite_fetion(params[:message])['send_message_result']}</result>"
      rescue
        render :xml => "<?xml version='1.0' encoding='UTF-8'?><errors><error>REQUEST_FAILED</error></errors>" 
        logger.error "#{@user.nick_name}尝试通过OpenApi发送短信失败: #{$!}:#{$@}"
        UserMailer.deliver_error_report(:title => "#{@user.nick_name}尝试通过OpenApi 发送短信失败",
          :error => "#{@user.nick_name} 尝试通过OpenApi发送短信失败: #{$!}:#{$@}"
        )
      end
    end
  end  
end
